public with sharing class AttachmentUploadController {
	public String recordId {get;set;}//记录ID 
    public String rename {get;set;}//附件重命名
    public String msg {get;set;}
    public Boolean isSuccess {get;set;}
    public AttachmentUploadController() {
        recordId = System.currentPageReference().getParameters().get('id');

    }

    public void save(){
        
        SavePoint sp = Database.setSavePoint();
    	try{
            String s = System.currentPageReference().getParameters().get('fileValue');
           Blob body = EncodingUtil.base64Decode(s.substring(s.indexOf(',')+1) );
           String format = System.currentPageReference().getParameters().get('fileFormat');
           if(s == null){
                msg = '请上传附件!';
                return ;
            }
           String attName = null;
            if(String.isBlank(rename) == false){
                attName = rename +'.'+ format;
            }else{
                attName = System.currentPageReference().getParameters().get('fileName') +'.'+ format;
            }
           
        	Attachment att = new Attachment();
            att.Name = attName;
            att.Body = body;
            att.ParentId = recordId;
            insert att;
            isSuccess = true;

            msg = '保存成功!';
            
    
        }catch(Exception e){
          	Database.rollback(sp);
            msg = '保存失败:'+e.getMessage();
         	//ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
        }
        
    }
}